package com.vhans.db.extend;

import com.vhans.core.strategy.SearchStrategy;
import com.vhans.core.web.model.vo.QuizSearchVO;
import com.vhans.core.web.model.vo.RecordSearchVO;
import com.vhans.db.mapper.BQuizMapper;
import com.vhans.db.mapper.BRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.vhans.core.constant.ElasticConstant.POST_TAG;
import static com.vhans.core.constant.ElasticConstant.PRE_TAG;

/**
 * MySQL搜索策略
 *
 * @author ican
 */
@Service("mySqlSearchStrategyImpl")
public class MysqlSearchStrategyImpl implements SearchStrategy {

    @Autowired
    private BRecordMapper recordMapper;

    @Autowired
    private BQuizMapper quizMapper;

    @Override
    public List<RecordSearchVO> searchRecord(String keyword) {
        if (StringUtils.isBlank(keyword)) {
            return new ArrayList<>();
        }
        List<RecordSearchVO> recordSearchVOList = recordMapper.searchRecord(keyword);
        return recordSearchVOList.stream()
                .map(record -> {
                    // 获取关键词第一次出现的位置
                    String recordContent = record.getContent();
                    int index = record.getContent().indexOf(keyword);
                    if (index != -1) {
                        // 获取关键词前面的文字
                        int preIndex = index > 25 ? index - 25 : 0;
                        String preText = record.getContent().substring(preIndex, index);
                        // 获取关键词到后面的文字
                        int last = index + keyword.length();
                        int postLength = record.getContent().length() - last;
                        int postIndex = postLength > 175 ? last + 175 : last + postLength;
                        String postText = record.getContent().substring(index, postIndex);
                        // 文章内容高亮
                        recordContent = (preText + postText).replaceAll(keyword, PRE_TAG + keyword + POST_TAG);
                    }
                    // 文章标题高亮
                    String recordTitle = record.getTitle().replaceAll(keyword, PRE_TAG + keyword + POST_TAG);
                    return RecordSearchVO.builder()
                            .id(record.getId())
                            .title(recordTitle)
                            .content(recordContent)
                            .build();
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<QuizSearchVO> searchQuiz(String keyword) {
        if (StringUtils.isBlank(keyword)) {
            return new ArrayList<>();
        }
        List<QuizSearchVO> quizSearchVOList = quizMapper.searchQuiz(keyword);
        return quizSearchVOList.stream()
                .map(quiz -> {
                    // 获取关键词第一次出现的位置
                    String quizContent = quiz.getContent();
                    int index = quiz.getContent().indexOf(keyword);
                    if (index != -1) {
                        // 获取关键词前面的文字
                        int preIndex = index > 25 ? index - 25 : 0;
                        String preText = quiz.getContent().substring(preIndex, index);
                        // 获取关键词到后面的文字
                        int last = index + keyword.length();
                        int postLength = quiz.getContent().length() - last;
                        int postIndex = postLength > 175 ? last + 175 : last + postLength;
                        String postText = quiz.getContent().substring(index, postIndex);
                        // 文章内容高亮
                        quizContent = (preText + postText).replaceAll(keyword, PRE_TAG + keyword + POST_TAG);
                    }
                    // 文章标题高亮
                    String recordTitle = quiz.getTitle().replaceAll(keyword, PRE_TAG + keyword + POST_TAG);
                    return QuizSearchVO.builder()
                            .id(quiz.getId())
                            .title(recordTitle)
                            .content(quizContent)
                            .build();
                })
                .collect(Collectors.toList());
    }
}
